package com.mdb.open.epay.service.epay.biz.service;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.mdb.bc.api.data.ResultEx;
import com.mdb.open.epay.common.MdbDataSource;
import com.mdb.open.epay.domain.po.epay.QpRefundOrder;
import com.mdb.open.epay.mapper.epay.QpOrderMapper;
import com.mdb.open.epay.mapper.epay.QpRefundOrderMapper;
import com.mdb.open.epay.service.epay.biz.bo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author liuyi
 * @date 2019/11/18
 **/
@Service
@DS(MdbDataSource.EPAY)
@Transactional
public class TradeBizService {

    @Autowired
    private QpOrderMapper qpOrderMapper;

    @Autowired
    private QpRefundOrderMapper qpRefundOrderMapper;

    /**
     * 交易成功
     */
    public ResultEx tradeSuccess(TradeSuccessBizBo bizBo) {
        int updateCount = qpOrderMapper.tradeSuccess(bizBo);
        if (updateCount != 1) {
            return ResultEx.fail();
        }
        return ResultEx.ok();
    }

    /**
     * 交易失敗
     */
    public ResultEx tradeFail(TradeFailBizBo bizBo) {
        int updateCount = qpOrderMapper.tradeFail(bizBo);
        if (updateCount != 1) {
            return ResultEx.fail();
        }
        return ResultEx.ok();
    }

    /**
     * 交易結果未知（超時）
     */
    public ResultEx tradeTimeout(TradeTimeoutBizBo bizBo) {
        int updateCount = qpOrderMapper.updateOrderTimeout(bizBo.getInnerOrderNo(),
                bizBo.getOldStatus(),
                bizBo.getNewStatus(),
                bizBo.getErrorCode(),
                bizBo.getErrorMsg());
        if (updateCount != 1) {
            return ResultEx.fail();
        }
        return ResultEx.ok();
    }

    /**
     * 創建退款訂單
     */
    public ResultEx createRefundOrder(CreateRefundOrderBizBo bizBo) {

        //新增退款訂單
        int insertCount = qpRefundOrderMapper.insert(bizBo.getQpRefundOrder());
        if (insertCount != 1) {
            return ResultEx.fail();
        }

        //修改原始訂單 增加退款中金額，增加退款中手續費
        int oriOrderRefundCount = qpOrderMapper.refund(bizBo.getQpRefundOrder().getInnerOriOrderNo(), bizBo.getQpRefundOrder().getAmount(), bizBo.getQpRefundOrder().getFee());
        if (oriOrderRefundCount != 1) {
            return ResultEx.fail();
        }

        return ResultEx.ok();
    }

    /**
     * 退款成功
     */
    public ResultEx refundSuccess(RefundSuccessBizBo bizBo) {
        QpRefundOrder qpRefundOrder = bizBo.getQpRefundOrder();
        //修改原始订单退款中金額，已退款金额，退款中手續費金額，已退手續費金額，
        //增加原订单退款金额，减少退款中金额
        //增加原订单退款手续费，减少退款中手续费金额
        int refundCount = qpOrderMapper.refundSuccess(qpRefundOrder.getInnerOriOrderNo(), qpRefundOrder.getAmount(), qpRefundOrder.getFee());
        if (refundCount != 1) {
            return ResultEx.fail();
        }
        // 修改退款訂單狀態
        int updateOrderStatusCount = qpRefundOrderMapper.refundSuccess(bizBo);
        if (updateOrderStatusCount != 1) {
            return ResultEx.fail();
        }

        return ResultEx.ok();
    }

    /**
     * 退款失敗
     */
    public ResultEx refundFail(RefundFailBizBo bizBo) {
        QpRefundOrder qpRefundOrder = bizBo.getQpRefundOrder();
        //修改原始订单退款中金額，已退款金额，退款中手續費金額，已退手續費金額，
        //减少原订单退款中金额
        //减少原订单退款中手续费金额
        int refundCount = qpOrderMapper.refundFail(qpRefundOrder.getInnerOriOrderNo(), qpRefundOrder.getAmount(), qpRefundOrder.getFee());
        if (refundCount != 1) {
            return ResultEx.fail();
        }
        // 修改退款訂單狀態
        int updateOrderStatusCount = qpRefundOrderMapper.refundFail(bizBo);
        if (updateOrderStatusCount != 1) {
            return ResultEx.fail();
        }

        return ResultEx.ok();
    }

    /**
     * 退款結果未知（超時）
     */
    public ResultEx refundTimeout(RefundTimeoutBizBo bizBo, String errorCode, String errorMsg) {
        // 修改退款訂單狀態
        int updateOrderStatusCount = qpRefundOrderMapper.updateOrderTimeout(bizBo.getInnerOrderNo(), bizBo.getOldStatus(), bizBo.getNewStatus(), errorCode, errorMsg);
        if (updateOrderStatusCount != 1) {
            return ResultEx.fail();
        }
        return ResultEx.ok();
    }

    /**
     * 創建撤銷訂單
     */
    public ResultEx createCancelOrder(CreateCancelOrderBizBo bizBo) {
        //新增退款訂單
        int insertCount = qpRefundOrderMapper.insert(bizBo.getQpRefundOrder());
        if (insertCount != 1) {
            return ResultEx.fail();
        }

        //修改原始訂單 增加退款中金額，增加退款中手續費
        int oriOrderRefundCount = qpOrderMapper.refund(bizBo.getQpRefundOrder().getInnerOriOrderNo(), bizBo.getQpRefundOrder().getAmount(), bizBo.getQpRefundOrder().getFee());
        if (oriOrderRefundCount != 1) {
            return ResultEx.fail();
        }
        return ResultEx.ok();
    }


}
